package com.demo2do.fms.admin.entity.search.supplier;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;
import com.demo2do.fms.entity.vehicle.AuditStatus;
import com.demo2do.fms.entity.vehicle.DeclareStatus;
import com.demo2do.fms.entity.vehicle.tractor.TransMean;

/**
 *
 * @author Ark
 *
 */
public class TractorSearch implements QueryBuilder {

    /** 多个牵引车中文车牌号 */
    private List<String> brandCNCodes;
    
    /** 牵引车中文车牌 */
    private String brandCNCode;
    
    /** 运输工具类型 */
    private TransMean transMean;
    
    /** 车辆审核状态 */
    private AuditStatus status;
    
    /** 申报审核状态 */
    private DeclareStatus declareStatus;
    
    /** 车辆GPS绑定SIM卡号 */
    private String simCard;
    
    /** 所属供方 */
    private Long supplierId;
    
    /** 所属供方代码 */
    private String supplierCode;
    
    /** 所属供方名称 */
    private String supplierName;
    
    /** 是否已绑定 */
    private Boolean bind;
    
    private QueryOrder queryOrder;
    
    private QueryFilter queryFilter;
    
    /**
     * The default constructor
     */
    public TractorSearch() {
        queryOrder = new QueryOrder("brandCNCode", "ASC");
    }

    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public QueryFilter getPreparedFilter(boolean refresh) {
        
        if (queryFilter == null || refresh) {
            
            queryFilter = new QueryFilter().addLike("brandCNCode", brandCNCode)
                                           .addEquals("transMean", transMean)
                                           .addEquals("status", status)
                                           .addEquals("declareStatus", declareStatus)
                                           .addLike("simCard", simCard)
                                           .addEquals("supplier.id", supplierId)
                                           .addEquals("supplier.code", supplierCode)
                                           .addLike("supplier.name", supplierName)
                                           .addEquals("bind", bind);
            
            if (CollectionUtils.isNotEmpty(brandCNCodes)) {
                queryFilter.addSentence("brandCNCode IN (:brandCNCodes)", "brandCNCodes", brandCNCodes);
            }
            
        }
        
        return queryFilter;
        
    }
    
    /**
     * @param supplierIds
     */
    public void addSupplierId(List<Long> supplierIds) {
        supplierIds.add(0L);
        this.getPreparedFilter(false).addSentence("supplier.id IN (:supplierIds)", "supplierIds", supplierIds);
    }
    
    /**
     * @param statuses
     */
    public void addStatus(List<AuditStatus> statuses) {
        this.getPreparedFilter(false).addSentence("status IN(:statuses)", "statuses", statuses);
    }

    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public QueryOrder getOrder() {
        return queryOrder;
    }

    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        String sentence = "FROM Tractor WHERE disabled = false" + getPreparedFilter(false).clauseSentence() + queryOrder.sentence();
        return sentence;
    }

    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).parameters();
    }

    /**
     * @return the brandCNCodes
     */
    public List<String> getBrandCNCodes() {
        return brandCNCodes;
    }

    /**
     * @return the brandCNCode
     */
    public String getBrandCNCode() {
        return brandCNCode;
    }

    /**
     * @return the transMean
     */
    public TransMean getTransMean() {
        return transMean;
    }

    /**
     * @return the status
     */
    public AuditStatus getStatus() {
        return status;
    }

    /**
     * @return the declareStatus
     */
    public DeclareStatus getDeclareStatus() {
        return declareStatus;
    }

    /**
     * @return the simCard
     */
    public String getSimCard() {
        return simCard;
    }

    /**
     * @return the supplierId
     */
    public Long getSupplierId() {
        return supplierId;
    }

    /**
     * @return the supplierCode
     */
    public String getSupplierCode() {
        return supplierCode;
    }

    /**
     * @return the supplierName
     */
    public String getSupplierName() {
        return supplierName;
    }
    
    /**
     * @return
     */
    public Boolean getBind() {
        return bind;
    }

    /**
     * @param brandCNCodes the brandCNCodes to set
     */
    public void setBrandCNCodes(List<String> brandCNCodes) {
        this.brandCNCodes = brandCNCodes;
    }

    /**
     * @param brandCNCode the brandCNCode to set
     */
    public void setBrandCNCode(String brandCNCode) {
        this.brandCNCode = brandCNCode;
    }

    /**
     * @param transMean the transMean to set
     */
    public void setTransMean(TransMean transMean) {
        this.transMean = transMean;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(AuditStatus status) {
        this.status = status;
    }

    /**
     * @param declareStatus the declareStatus to set
     */
    public void setDeclareStatus(DeclareStatus declareStatus) {
        this.declareStatus = declareStatus;
    }

    /**
     * @param simCard the simCard to set
     */
    public void setSimCard(String simCard) {
        this.simCard = simCard;
    }

    /**
     * @param supplierId the supplierId to set
     */
    public void setSupplierId(Long supplierId) {
        this.supplierId = supplierId;
    }

    /**
     * @param supplierCode the supplierCode to set
     */
    public void setSupplierCode(String supplierCode) {
        this.supplierCode = supplierCode;
    }

    /**
     * @param supplierName the supplierName to set
     */
    public void setSupplierName(String supplierName) {
        this.supplierName = supplierName;
    }
    
    /**
     * @param bind
     */
    public void setBind(Boolean bind) {
        this.bind = bind;
    }
    
}